package hlhj.fhp.checkpoint.atys

import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.liaoinstan.springview.container.DefaultFooter
import com.liaoinstan.springview.container.DefaultHeader
import com.liaoinstan.springview.widget.SpringView
import hlhj.fhp.checkpoint.BaseAty
import hlhj.fhp.checkpoint.R
import hlhj.fhp.checkpoint.adp.CostListAdp
import hlhj.fhp.checkpoint.iview.CostView
import hlhj.fhp.checkpoint.javaBean.CostBean
import hlhj.fhp.checkpoint.presenter.CostPresenter
import kotlinx.android.synthetic.main.aty_cost_list.*
import kotlinx.android.synthetic.main.lo_pub_head.*
import kotlinx.android.synthetic.main.lo_publist.*

/**
 * Created by Never Fear   on 2018\11\12 0012.
Never More....
 */
class CostAty :BaseAty<CostView,CostPresenter>(), CostView, SpringView.OnFreshListener {
    private val datas=ArrayList<CostBean.InfoBean.DataBean>()
    private var adp:CostListAdp?=null
    private var page=1
    private var oid=""
    private var money=""
    private var offer=""
    override fun setCostDetail(costBean: CostBean) {
        spView?.onFinishFreshAndLoad()
        if (page==1){
            datas.clear()
        }
        datas.addAll(costBean.info.data)
        adp?.notifyDataSetChanged()
    }

    override fun initView() {
        tvTittle.setText("${intent.getStringExtra("num")}")
        btRightText.setText("全部支付")
        btRightText.visibility=View.VISIBLE


        adp= CostListAdp(datas)
        listView.adapter=adp
        listView.layoutManager=LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)

        spView.footer=DefaultFooter(this)
        spView.header=DefaultHeader(this)
    }

    override fun logicStart() {
        presenter?.getCostDetail(intent.getIntExtra("id",-1),page,this)
    }

    override fun setListener() {
        btAll.setOnClickListener {
            for (i in 0..datas.size-1){
                if (datas[i].status==2){
                    if (oid.equals("")){
                        oid=datas[i].id.toString()
                        money=datas[i].offer.toString()
                        offer=(datas[i].serviceFee.toDouble()+datas[i].markFee.toDouble()).toString()
                    }else{
                        oid=oid+",${datas[i].id}"
                        money=(money.toDouble()+datas[i].offer.toDouble()).toString()
                        offer=(offer.toDouble()+datas[i].serviceFee.toDouble()+datas[i].markFee.toDouble()).toString()
                    }
                }

            }
            if (oid.equals(""))return@setOnClickListener
            val intent = Intent(this, PayAty::class.java)
            intent.putExtra("id",oid)
            intent.putExtra("money",money)
            intent.putExtra("offer",offer)
            intent.putExtra("orderType",0)
            startActivityForResult(intent,GOTOFEE)
            oid=""
            money=""
            offer=""
        }
        spView.setListener(this)
        btRightText.setOnClickListener {
            for (i in 0..datas.size-1){
                if (datas[i].status==2){
                    if (oid.equals("")){
                        oid=datas[i].id.toString()
                        money=datas[i].offer.toString()
                        offer=(datas[i].serviceFee.toDouble()+datas[i].markFee.toDouble()).toString()
                    }else{
                        oid=oid+",${datas[i].id}"
                        money=(money.toDouble()+datas[i].offer.toDouble()).toString()
                        offer=(offer.toDouble()+datas[i].serviceFee.toDouble()+datas[i].markFee.toDouble()).toString()
                    }
                }

            }
            if (oid.equals(""))return@setOnClickListener
            val intent = Intent(this, PayAty::class.java)
            intent.putExtra("id",oid)
            intent.putExtra("money",money)
            intent.putExtra("offer",offer)
            intent.putExtra("orderType",0)
            startActivityForResult(intent,GOTOFEE)
            oid=""
            money=""
            offer=""
        }
    }

    override fun bindView(): CostView {
        return this
    }

    override fun onLoadmore() {
        page++
        presenter?.getCostDetail(intent.getIntExtra("id",-1),page,this)

    }

    override fun onRefresh() {
        page=1
        presenter?.getCostDetail(intent.getIntExtra("id",-1),page,this)

    }

    override fun createPresenter(): CostPresenter {
        return CostPresenter()
    }

    private val GOTOFEE=12126

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode!=-1)return
        when(requestCode){
            GOTOFEE->{
                page=1
                presenter?.getCostDetail(intent.getIntExtra("id",-1),page,this)
            }
        }
    }
    override val contentId: Int= R.layout.aty_cost_list
}